//
//  MTHomeViewController.m
//  美团HD
//
//  Created by kongxiaopeng on 15/6/18.
//  Copyright (c) 2015年 njsk. All rights reserved.
//

#import "MTHomeViewController.h"
#import "MTConst.h"
#import "UIBarButtonItem+Extension.h"
#import "UIView+Extension.h"
#import "MTHomeTopItem.h"
#import "MTCategoryViewController.h"
#import "MTRegionViewController.h"
#import "MTCategory.h"
#import "MTSort.h"
#import "MTCity.h"
#import "MTMetaTool.h"
#import "MTRegion.h"
#import "MTSortViewViewController.h"
#import "MTNavigationController.h"
#import "MTSearchViewController.h"
#import "MJRefresh.h"

@interface MTHomeViewController ()
/** 分类item */
@property (nonatomic, weak) UIBarButtonItem *categoryItem;
/** 地区item */
@property (nonatomic, weak) UIBarButtonItem *regionItem;
/** 排序item */
@property (nonatomic, weak) UIBarButtonItem *sortItem;

/** 当前选中的城市名字 */
@property (nonatomic, copy) NSString *selectedCityName;
/** 当前选中的分类名字 */
@property (nonatomic, copy) NSString *selectedCategoryName;
/** 当前选中的区域名字 */
@property (nonatomic, copy) NSString *selectedRegionName;
/** 当前选中的排序 */
@property (nonatomic, strong) MTSort *selectedSort;

/** 分类popover */
@property (nonatomic, strong) UIPopoverController *categoryPopover;
/** 区域popover */
@property (nonatomic, strong) UIPopoverController *regionPopover;
/** 排序popover */
@property (nonatomic, strong) UIPopoverController *sortPopover;


@end

@implementation MTHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    //监听城市改变
    [MTNotificationCenter addObserver:self selector:@selector(cityDidChange:) name:MTCityDidChangeNotification object:nil];
    
    //监听排序改变
    [MTNotificationCenter addObserver:self selector:@selector(sortDidChange:) name:MTSortDidChangeNotification object:nil];
    
    //监听分类改变
    [MTNotificationCenter addObserver:self selector:@selector(categoryDidChange:) name:MTCategoryDidChangeNotification object:nil];
    
    //监听区域改变
    [MTNotificationCenter addObserver:self selector:@selector(regionDidChange:) name:MTRegionDidChangeNotification object:nil];
    
    //设置导航栏内容
    [self setupLeftNav];
    [self setupRighrNav];
    

}

-(void)dealloc{
    [MTNotificationCenter removeObserver:self];
}

#pragma mark - 监听通知
- (void)cityDidChange:(NSNotification*)notification{
    self.selectedCityName = notification.userInfo[MTSelectCityName];
    
    //1.更换顶部区域item的文字
    MTHomeTopItem* topItem = (MTHomeTopItem*)self.regionItem.customView;
    [topItem setTitle:[NSString stringWithFormat:@"%@ - 全部",self.selectedCityName]];
    [topItem setSubtitle:nil];
    
    //2.刷新表格数据
    [self.collectionView headerBeginRefreshing];
}

- (void)categoryDidChange:(NSNotification*)notification{
    MTCategory* category = notification.userInfo[MTSelectCategory];
    NSString* subcategoryName = notification.userInfo[MTSelectSubcategoryName];
    
    if (subcategoryName == nil || [subcategoryName isEqualToString:@"全部"]) {//点击的数据没有子分类
        self.selectedCategoryName = category.name;
    }else{
        self.selectedCategoryName = subcategoryName;
    }
    
    if ([self.selectedCategoryName isEqualToString:@"全部分类"]) {
        self.selectedCategoryName = nil;
    }
    
    //1.更换顶部区域item的文字
    MTHomeTopItem* topItem = (MTHomeTopItem*)self.categoryItem.customView;
    [topItem setIcon:category.icon highIcon:category.highlighted_icon];
    [topItem setTitle:category.name];
    [topItem setSubtitle:subcategoryName];
    
    //2.关闭popover
    [self.categoryPopover dismissPopoverAnimated:YES];
    
    //3.刷新表格数据
    [self.collectionView headerBeginRefreshing];

}

- (void)regionDidChange:(NSNotification*)notification{
    MTRegion* region = notification.userInfo[MTSelectRegion];
    NSString* subregionName = notification.userInfo[MTSelectSubregionName];
    
    if (subregionName == nil || [subregionName isEqualToString:@"全部"]) {
        self.selectedRegionName = region.name;
    }else{
        self.selectedRegionName = subregionName;
    }
    
    if ([self.selectedRegionName isEqualToString:@"全部"]) {
        self.selectedRegionName = nil;
    }
    
    //1.更换顶部区域item的文字
    MTHomeTopItem* topItem = (MTHomeTopItem*)self.regionItem.customView;
    [topItem setTitle:[NSString stringWithFormat:@"%@ - %@",self.selectedCityName,region.name]];
    [topItem setSubtitle:subregionName];
    
    //2.关闭popover
    [self.regionPopover dismissPopoverAnimated:YES];
    
    //3.刷新表格数据
    [self.collectionView headerBeginRefreshing];
}

- (void)sortDidChange:(NSNotification*)notification{
    self.selectedSort = notification.userInfo[MTSelectSort];
    
    // 1.更换顶部排序item的文字
    MTHomeTopItem *topItem = (MTHomeTopItem *)self.sortItem.customView;
    [topItem setSubtitle:self.selectedSort.label];
    
    // 2.关闭popover
    [self.sortPopover dismissPopoverAnimated:YES];
    
    //3.刷新表格数据
    [self.collectionView headerBeginRefreshing];
}

#pragma mark - 实现父类提供的方法
-(void)setupParams:(NSMutableDictionary *)params{
    //城市
    params[@"city"] = self.selectedCityName;
    //分类（类别）
    if (self.selectedCategoryName) {
        params[@"category"] = self.selectedCategoryName;
    }
    //区域
    if (self.selectedRegionName) {
        params[@"region"] = self.selectedRegionName;
    }
    //排序
    if (self.selectedSort) {
        params[@"sort"] = @(self.selectedSort.value);
    }
}


- (void)setupLeftNav{
    //1.LOGO
    UIBarButtonItem* logoItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_meituan_logo"] style:UIBarButtonItemStyleDone target:nil action:nil];
    logoItem.enabled = NO;
    
    //2.类别
    MTHomeTopItem* categoryTopItem = [MTHomeTopItem item];
    [categoryTopItem addTarget:self action:@selector(categoryClick)];
    UIBarButtonItem* categoryItem = [[UIBarButtonItem alloc]initWithCustomView:categoryTopItem];
    self.categoryItem = categoryItem;
    
    
    // 3.地区
    MTHomeTopItem *regionTopItem = [MTHomeTopItem item];
    [regionTopItem addTarget:self action:@selector(districtClick)];
    UIBarButtonItem *regionItem = [[UIBarButtonItem alloc] initWithCustomView:regionTopItem];
    self.regionItem = regionItem;
    
    // 4.排序
    MTHomeTopItem *sortTopItem = [MTHomeTopItem item];
    [sortTopItem setTitle:@"排序"];
    [sortTopItem setIcon:@"icon_sort" highIcon:@"icon_sort_highlighted"];
    [sortTopItem addTarget:self action:@selector(sortClick)];
    UIBarButtonItem *sortItem = [[UIBarButtonItem alloc] initWithCustomView:sortTopItem];
    self.sortItem = sortItem;
    
    self.navigationItem.leftBarButtonItems = @[logoItem,categoryItem,regionItem,sortItem];
}

- (void)setupRighrNav{
    UIBarButtonItem *mapItem = [UIBarButtonItem itemWithTarget:nil action:nil image:@"icon_map" highImage:@"icon_map_highlighted"];
    mapItem.customView.width = 60;
    
    UIBarButtonItem *searchItem = [UIBarButtonItem itemWithTarget:self action:@selector(search) image:@"icon_search" highImage:@"icon_search_highlighted"];
    searchItem.customView.width = 60;
    self.navigationItem.rightBarButtonItems = @[mapItem, searchItem];
}

#pragma mark - 顶部item点击方法
- (void)search{
    MTNavigationController* nav = [[MTNavigationController alloc]initWithRootViewController:[[MTSearchViewController alloc]init]];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)categoryClick{
    //显示分类菜单
    self.categoryPopover = [[UIPopoverController alloc]initWithContentViewController:[[MTCategoryViewController alloc]init]];
    
    [self.categoryPopover presentPopoverFromBarButtonItem:self.categoryItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

- (void)districtClick
{
    MTRegionViewController* region = [[MTRegionViewController alloc]init];
    if (self.selectedCityName) {
        //获得当前选中的城市
        MTCity* city = [[[MTMetaTool cities]filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = %@",self.selectedCityName]]firstObject];
        region.regions = city.regions;
    }
    
    
    //显示区域菜单
    self.regionPopover = [[UIPopoverController alloc]initWithContentViewController:region];
    
    [self.regionPopover presentPopoverFromBarButtonItem:self.regionItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    region.popover = self.regionPopover;
}

- (void)sortClick
{
    //显示排序菜单
    self.sortPopover = [[UIPopoverController alloc]initWithContentViewController:[[MTSortViewViewController alloc]init]];
    
    [self.sortPopover presentPopoverFromBarButtonItem:self.sortItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}





@end
